<?php


namespace App\Http\Controllers\Business;


use App\Http\Controllers\Controller;
use App\Models\BusinessUser;
use App\Models\SmsLog;
use Illuminate\Http\Request;
use Overtrue\EasySms\EasySms;

class AdminUserController extends Controller
{

    public function register(Request $request)
    {


        $username = $request->input('username');
        $pass = passwordEncode($request->input('pass'));
        $code = $request->input('code');
        $admin = BusinessUser::where('username', $username)->first();
        if ($admin) {
            return $this->makeResponse([], '帐号存在', 500);
        }

        BusinessUser::createOne([
            'username' => $username,
            'password' => $pass,
        ])->save();
        return $this->makeResponse([], '注册成功', 200);

    }

    public function login(Request $request)
    {
        $pass = $request->input('pass');
        $username = $request->input('username');

        $admin = BusinessUser::with('shop')->where('username', $username)->first();

        if (!$admin) {
            return $this->makeResponse([], '帐号不存在', 500);
        }
        if (!passwordVerify($pass, $admin->password)) {
            return $this->makeResponse([], '密码错误', 500);
        }
        unset($admin->password);
        return $this->makeResponse($admin, '登陆成功', 200);

    }
    public function sendSms(Request $request)
    {

        try {
            $phone = $request->input('phone');
//            $type = $request->input('type');
//            $smsConfig = config('sms');
//            $sms = new EasySms($smsConfig);
//            $code = rand(1000, 9999);
//            $data = [
////                'content'  => '您的验证码为: 6379',
//                'template' => 'SMS_175240424',
//                'data' => [
//                    'code' => $code
//                ]
//            ];
//            $data = $sms->send($phone, $data);
//            $data = ['phone' => $phone, 'code' => $code];
//            SmsLog::createOne($data)->save();
            return makeResponse([], '发送成功', 200);
        } catch (\Exception $exception) {
            return makeResponse([], "[-FILE-]:" . $exception->getFile() . "[-LINE-]:" . $exception->getLine(),
                500);
        }
    }

    function request_by_curl($remote_server, $post_string)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    public function dingding(Request $request)
    {
        $message = $request->input('msg');
        $token = 'a332104089db688c02a9420cfa22944109efaff2b0000ece6907ac4f8b206136';
        $se = 'SECc8a262406c941b0062b8eaa37e30787142eb3f436c2243fdec7afbeb42c6b9a9';
        $sign = $this->hashEode($se);
        $webhook = "https://oapi.dingtalk.com/robot/send?access_token=" . $token . "&timestamp=" . $sign['timestamp'] . '&sign=' . $sign['sign'];
//        $message = "我就是我, 是不一样的烟火";
        $data = [
            'msgtype' => 'text',
            'text' =>
                [
                    'content' => $message
                ],
            "at" => [
                "atMobiles" => [

                ],
                "isAtAll" => false
            ]
        ];
        $data_string = json_encode($data);
        $result = $this->request_by_curl($webhook, $data_string);
        echo $result;
    }

    public function hashEode($data)
    {
        $time = time() * 1000;
        $sign = urlencode(base64_encode(hash_hmac("sha256", $time . "\n" . $data, $data, true)));
        return ['timestamp' => $time, 'sign' => $sign];
    }

}